import { ref,computed } from "vue";
import { addition } from "@/utils/compute.js";
import { ElMessage } from "element-plus";
export const useBillHooks = () => {
  const cExtraChargesRef = ref();
  const cStorageChargeRef = ref();
  const cServiceChargeRef = ref();
  const cExportChargesRef = ref();
  const cBulkPaymentRef = ref();
  const multipleTableRef = ref();
  const multiples = ref([]);

  const sumNumber = computed(() => {
    return (multiples.value || []).length;
  });
  const sumAccount = computed(() => {
    return (multiples.value || []).reduce((n, x) => {
      return addition(n, x.amount);
    }, 0);
  });

  const getRowKey = (item) => {
    return item.id;
  };
  const handlePayment = () => {
    if (multiples.value.length <= 0) {
      ElMessage.warning("请选择批量项");
      return;
    }
    cBulkPaymentRef.value.handleOpen(multiples.value);
  };
  const onCheckSelectable = (item) => {
    if (item.status === 0) {
      // 未支付
      return true;
    }
    return false;
  };
  const getDetailOpen = (type, res) => {
    switch (type) {
      case 8:
        // 仓储
        cStorageChargeRef.value.handleOpen(res);
        break;
      case 9:
        // 平台服务费
        cServiceChargeRef.value.handleOpen(res);
        break;
      case 11:
        // 额外费用
        cExtraChargesRef.value.handleOpen(res);
        break;
      default:
        // 检测费、上牌、报关和装箱费用
        cExportChargesRef.value.handleOpen(res);
        break;
    }
  };
  const handleSelectionChange = (item) => {
    multiples.value = item;
  };
  return {
    cExtraChargesRef,
    cStorageChargeRef,
    cServiceChargeRef,
    cExportChargesRef,
    cBulkPaymentRef,
    multipleTableRef,
    multiples,
    sumNumber,
    sumAccount,
    getDetailOpen,
    handlePayment,
    getRowKey,
    onCheckSelectable,
    handleSelectionChange
  };
};
